package com.th.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.th.system.domain.UsWalletInfo;
import com.th.system.service.IUsWalletInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.th.common.annotation.Log;
import com.th.common.core.controller.BaseController;
import com.th.common.core.domain.AjaxResult;
import com.th.common.enums.BusinessType;
import com.th.system.domain.UsWalletTransaction;
import com.th.system.service.IUsWalletTransactionService;
import com.th.common.utils.poi.ExcelUtil;
import com.th.common.core.page.TableDataInfo;

/**
 * 提现审核Controller
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/transaction")
public class UsWalletTransactionController extends BaseController
{
    @Resource
    private IUsWalletTransactionService usWalletTransactionService;

    @Resource
    private IUsWalletInfoService usWalletInfoService;
    /**
     * 查询提现审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:list')")
    @GetMapping("/list")
    public TableDataInfo list(UsWalletTransaction usWalletTransaction)
    {
        startPage();
        List<UsWalletTransaction> list = usWalletTransactionService.selectUsWalletTransactionList(usWalletTransaction);
        return getDataTable(list);
    }

    /**
     * 导出提现审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:export')")
    @Log(title = "提现审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UsWalletTransaction usWalletTransaction)
    {
        List<UsWalletTransaction> list = usWalletTransactionService.selectUsWalletTransactionList(usWalletTransaction);
        ExcelUtil<UsWalletTransaction> util = new ExcelUtil<UsWalletTransaction>(UsWalletTransaction.class);
        util.exportExcel(response, list, "提现审核数据");
    }

    /**
     * 获取提现审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(usWalletTransactionService.selectUsWalletTransactionById(id));
    }

    /**
     * 新增提现审核
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:add')")
    @Log(title = "提现审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UsWalletTransaction usWalletTransaction)
    {
        return toAjax(usWalletTransactionService.insertUsWalletTransaction(usWalletTransaction));
    }

    /**
     * 修改提现审核
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:edit')")
    @Log(title = "提现审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UsWalletTransaction usWalletTransaction)
    {
        if(usWalletTransaction.getStatus()==2){
            //退回提现款
            UsWalletInfo usWalletInfo=usWalletInfoService.selectUsWalletInfoById(usWalletTransaction.getWalletId());
            usWalletInfo.setBalance(usWalletInfo.getBalance()+usWalletTransaction.getAmountMoney());
            usWalletInfo.setTotalExpenditure(usWalletInfo.getTotalExpenditure()-usWalletTransaction.getAmountMoney());
            usWalletInfoService.updateUsWalletInfo(usWalletInfo);
            //生成记录
            UsWalletTransaction usWalletTransaction1=new UsWalletTransaction();
            usWalletTransaction1.setWalletId(usWalletTransaction.getWalletId());
            usWalletTransaction1.setPaymentInfo("提现失败退回原路");
            usWalletTransaction1.setStatus(1);
            usWalletTransaction1.setType(1);
            usWalletTransaction1.setAmountMoney(usWalletTransaction.getAmountMoney());
            usWalletTransaction1.setPaymentMethod(0);
            usWalletTransactionService.insertUsWalletTransaction(usWalletTransaction1);
        }
        return toAjax(usWalletTransactionService.updateUsWalletTransaction(usWalletTransaction));
    }

    /**
     * 删除提现审核
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:remove')")
    @Log(title = "提现审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(usWalletTransactionService.deleteUsWalletTransactionByIds(ids));
    }
}
